package com.yf.mediaserver.gb.sip.processors.resp.impl;


import com.yf.mediaserver.gb.cmd.SIPRequestHeaderProvider;
import com.yf.mediaserver.gb.sip.model.Gb28181Sdp;
import com.yf.mediaserver.gb.sip.processors.SIPProcessorObserver;
import com.yf.mediaserver.gb.sip.processors.SIPSender;
import com.yf.mediaserver.gb.sip.processors.resp.SIPResponseProcessorAbstract;
import com.yf.mediaserver.gb.sip.util.SipUtils;
import gov.nist.javax.sip.ResponseEventExt;
import gov.nist.javax.sip.message.SIPResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sdp.SdpParseException;
import javax.sdp.SessionDescription;
import javax.sip.ResponseEvent;
import javax.sip.message.Response;


/**
 * 处理INVITE响应  通常为自己平台发送的INVITE响应
 */
@Slf4j
@Component
public class InviteResponseProcessor extends SIPResponseProcessorAbstract {

    private final String method = "INVITE";

    @Resource
    private SIPProcessorObserver sipProcessorObserver;


    @Resource
    private SIPSender sipSender;

    @Resource
    private SIPRequestHeaderProvider headerProvider;


    @Override
    public void afterPropertiesSet() {
        // 添加消息处理的订阅
        sipProcessorObserver.addResponseProcessor(method, this);
    }


    /**
     * 处理invite响应
     *
     * @param evt 响应消息
     */
    @Override
    public void process(ResponseEvent evt) {
        log.debug("接收到消息：" + evt.getResponse());
        try {
            SIPResponse response = (SIPResponse) evt.getResponse();
            int statusCode = response.getStatusCode();
            // trying不会回复
            if (statusCode == Response.TRYING) {
                log.warn("不回复trying");
            }
            // 成功响应
            // 下发ack
            if (statusCode == Response.OK) {
                ResponseEventExt event = (ResponseEventExt) evt;

                String contentString = new String(response.getRawContent());
                Gb28181Sdp gb28181Sdp = SipUtils.parseSDP(contentString);

                //v=0
                //o=34020000001320000001 1205 1205 IN IP4 192.168.31.68
                //s=Play
                //c=IN IP4 192.168.31.68
                //t=0 0
                //m=video 15060 RTP/AVP 96
                //a=sendonly
                //a=rtpmap:96 PS/90000
                //a=filesize:0
                SessionDescription sdp = gb28181Sdp.getBaseSdb();
                log.info("sdp.getOrigin().getUsername() :{}", sdp.getOrigin().getUsername());
                log.info("event.getRemoteIpAddress() :{}", event.getRemoteIpAddress());
                log.info("event.getRemotePort() :{}", event.getRemotePort());
//				SipURI requestUri = ADDRESS_FACTORY.createSipURI(sdp.getOrigin().getUsername(), event.getRemoteIpAddress() + ":" + event.getRemotePort());
                String channelId = "34020000001320000001";
                String deviceAddr = "192.168.1.65:5061";
                headerProvider.sendInviteRespACK(response, channelId, deviceAddr);

                log.info("[回复ack] {}-> {}:{} ", sdp.getOrigin().getUsername(), event.getRemoteIpAddress(), event.getRemotePort());
            }
        } catch (SdpParseException e) {
            log.info("[点播回复ACK]，异常：", e);
        }
    }

}
